#pragma once

#include <string>
#include <list>
#include <WinSock2.h>

namespace SocketCtrl4
{

class AFX_EXT_CLASS TCP
{
public:
	TCP(void);

	~TCP(void);

public:
	SOCKET socket(SOCKET sock = INVALID_SOCKET);

	void close();

	int bind(int port = 0, const std::string &ip = ""); 

	int listen(int backlog);

	SOCKET accept(std::string &ip, int &port);

	int connect(const std::string &ip, int port);

	int recv(char *buf, int len, int flags = 0);

	int send(const char *buf, int len, int flags = 0);

	int getsocketopt(int level, int optname, char *optval, int *optlen);

	int setsocketopt(int level, int optname, const char FAR * optval, int optlen);

	int getpeername(std::string &ip, int &port);

	int getsockname(std::string &ip, int &port);

	int WSAAsyncSelect(HWND hWnd, unsigned int wMsg, long lEvent);

private:
	SOCKET _socket;
};

class TCPServer
{
public:
	TCPServer();

	~TCPServer();

public:
	void start(TCP *tcp);

	void stop();

private:
	TCP	*_tcp;
};

class TCPMgr
{
public:
	TCPMgr();

	~TCPMgr();

public:
	TCP *create();

	void destory(TCP *tcp);

	TCP *get(size_t index);

private:
	std::list<TCP *>	_tcps;
};

}